package com.yantong.service.impl;

import com.yantong.mapper.WebAddressMapper;
import com.yantong.pojo.WebAddress;
import com.yantong.utils.PageUtils;
import com.yantong.pojo.Address;
import com.yantong.mapper.AddressMapper;
import com.yantong.service.AddressService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressMapper addressMapper;

    @Autowired
    private WebAddressMapper webAddressMapper;

    private static final Logger logger = LoggerFactory.getLogger(AddressServiceImpl.class);

    @Override
    public PageUtils<Address> list(Map params) {
        PageUtils<Address> pageUtils = new PageUtils<>();

        int total = addressMapper.queryTotal(params);
        if (total > 0) {
            pageUtils.setTotal(total);
            pageUtils.setRows(addressMapper.queryList(params));
        }
        return pageUtils;
    }

    @Override
    public int save(Address entity) {
        int result = addressMapper.insertSelective(entity);
        if(result >= 0){
            if(entity.getIsDefault() != null && entity.getIsDefault() == 1){
                addressMapper.updateDefault(entity);
            }
        }

        return result;
    }

    @Override
    public Address getByKey(Long id) {
        return addressMapper.selectByPrimaryKey(id);
    }

    @Override
    public int update(Address entity) {
        int result = addressMapper.updateByPrimaryKeySelective(entity);
        if(result >= 0){
            if(entity.getIsDefault() != null && entity.getIsDefault() == 1){
                addressMapper.updateDefault(entity);
            }
        }
        return result;
    }

    @Override
    public List<Address> queryList(Map params) {
        return addressMapper.queryList(params);
    }

    @Override
    public void delete(Long id) {
        addressMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int queryTotal(Map params) {
        return addressMapper.queryTotal(params);
    }

    @Override
    public Address getMemDefaultAddress(Long mid) {
        return addressMapper.getMemDefaultAddress(mid);
    }

    @Override
    public Address getLateLyAddress(Map map) {
        return addressMapper.getLateLyAddress(map);
    }

    @Override
    public int saveWebAddress(WebAddress webAddress) {
        return webAddressMapper.insertSelective(webAddress);
    }


}
